package com.res.share.filter;

import com.res.share.common.Constants;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInteceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        //判断是否已有该用户登录的session
        if(session.getAttribute(Constants.CURRENT_USER) !=null){
            return true;
        }
        String redirectUrl = request.getServletPath();
        if(StringUtils.isNotEmpty(redirectUrl)){
            //跳转到登录页
            String url = "/to/login.html?redirectUrl="+redirectUrl;
            response.sendRedirect(url);
        }else {
            //跳转到登录页
            String url = "/to/login.html";
            response.sendRedirect(url);
        }

        return false;
    }

}
